﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using DotConferenceSql;
using NUnit.Framework.SyntaxHelpers;
using System.Data.SqlClient;
using System.Data;

namespace TestDotConferenceSql
{
    [TestFixture]
    public class TestUpdateQueryElement
    {
        SqlQuery query;
        UpdateQueryElement updateElement;

        [SetUp]
        public void Init()
        {
            query = new SqlQuery();
            updateElement = new UpdateQueryElement();
        }

        [Test]
        public void BasicUpdateQuery()
        {
            updateElement.Table = "Test";
            updateElement.Add("field1", 1);
            updateElement.Add("field2", "String");
            updateElement.Add("field3", true);
            updateElement.Add("field4", DateTime.Parse("2009-01-01"));

            query.Elements.Add(updateElement);

            Assert.That(query.BuildTextQuery(), Text.Contains("UPDATE Test SET field1 = @field1,field2 = @field2,field3 = @field3,field4 = @field4"));

            List<SqlParameter> parameters = query.ListParameters();

            Assert.That(parameters.Count, Is.EqualTo(4));
           
            Assert.That(parameters[0].DbType, Is.EqualTo(DbType.Int32));
            Assert.That(parameters[1].DbType, Is.EqualTo(DbType.String));
            Assert.That(parameters[2].DbType, Is.EqualTo(DbType.Boolean));
            Assert.That(parameters[3].DbType, Is.EqualTo(DbType.DateTime));
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void UpdateQueryWithNoField()
        {
            updateElement.Table = "Test";

            query.Elements.Add(updateElement);

            query.BuildTextQuery();
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void UpdateQueryWithNoTable()
        {
            updateElement.Add("field1", 1);

            query.Elements.Add(updateElement);

            query.BuildTextQuery();
        }
    }
}
